<?php

namespace App\Http\Middleware;

use App\Utils\Message;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class AuthMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {

        // 获取登录用户信息
        $user = Auth::guard('api')->user();
        
        if(empty($user)) { // 未获取到有效的用户信息
            return Message::show_message(false, '暂未登录，无权查看',[], 401);
        }

        // 判断权限，目前写死，因为不会开放其他用户组
        if($user->gid !== 1) {
            return Message::show_message(false, '暂无权操作');
        }

        return $next($request);
    }
}
